﻿Chenxin.UI.BillingForm = Chenxin.extend(Chenxin.UI.FormCommon, {
    actionUrl: '/service/logic/ShowListIntent.ashx',
    id: 'jqm_billingform',
    jqmWidth: 700,
    jqmHeight: 460,
    defBBtn: true,
    createForm: function() {
        return Chenxin.FormCommon.billingForm(this.id);
    },
    customerFormVal: function(json) {
        if (!json.GetedTime) {
            alert('收取时间不能为空！');
            return false;
        }
        var _count = 0;
        for (var i = 0; i < this.drCount; i++) {
            if (json['PayMode_' + i] != undefined && json['PayMoney_' + i] != undefined) {
                if ((json['PayMode_' + i] == 'POS机刷卡' || json['PayMode_' + i] == '支/本票') && (json['PayCode_' + i] == undefined || json['PayName_' + i] == undefined)) {
                    alert('当支付方式为“POS机刷卡”或“支/本票”时，付款方姓名和付款方帐号必填');
                    return false;
                }
                if (json['PayMoney_' + i].isNum()) {
                    _count += parseFloat(json['PayMoney_' + i]);
                    delete json['PayMode_' + i];
                    delete json['PayMoney_' + i];
                    delete json['PayName_' + i];
                    delete json['PayCode_' + i];
                    delete json['PayMemo_' + i];
                } else {
                    alert('金额栏内容必须为有效的数字');
                    return false;
                }
            }
        }
        if (_count != json.GetedMoney) {
            alert('明细金额之和必须等于本次开单金额！');
            return false;
        }


        if (parseFloat(json.GetedMoney) > parseFloat(json.TotalMoney)) {
            alert('开单金额必须小于或等于总诚意金额！');
            return false;
        }
        if (parseFloat(json.GetedMoney) == "0") {
            alert('开单金额必须大于0！');
            return false;
        }
        if (parseFloat(json.IsGetedMoney) >= parseFloat(json.TotalMoney)) {
            alert('此次开单金额与已开单金额之和不能超过总额！');
            return false;
        }
        else if (parseFloat(json.IsGetedMoney) + parseFloat(json.GetedMoney) > parseFloat(json.TotalMoney)) {
            alert('此次开单金额与已开单金额之和不能超过总额！');
            return false;
        }
        delete json.IsGetedMoney;
        delete json.TotalMoney;

        return true;
    },
    createComboList: function() {
        //门店
        var comboGetedDepartId = new Chenxin.UI.StructCombo({
            param: { op: 'getcontroldeparts' },
            containerId: '#GetedDepartId',
            name: 'GetedDepartId',
            inModelType: 'all',
            txtModelName: 'GetedDepart',
            required: true,
            tbxWidth: 129,
            colNum: 4,
            hasPage: true,
            hasSearch: true,
            onSelected: function() {
                if (this.getValue() != this.defVal) {
                    comboGetedEmployeeId.loadUser(this.getValue());
                }
            },
            afterSetValue: function(newVal, oldVal) {
                if (newVal != this.defVal) {
                    comboGetedEmployeeId.loadUser(newVal);
                }
            }
        });
        //店长
        var comboGetedEmployeeId = new Chenxin.UI.RichModelTreeCombo({
            textBoxId: 'GetedEmployeeId',
            containerId: '#GetedEmployeeId',
            inModelType: 'all',
            txtModelName: 'GetedEmployee',
            required: false,
            singleSelect: true,
            name: 'GetedEmployeeId'
        });
        return [comboGetedDepartId, comboGetedEmployeeId];
    },
    initEvent: function() {
        var _this = this;
        $('#btn_add_row').live('click', function() {
            _this.createIptRow();
        });

        $('.a_del_row').live('click', function() {
            $(this.parentNode.parentNode).remove();
            for (var i = 0; i < _this.comboList; i++) {
                if (_this.comboList[i].name == this.id.replace('a_del', 'PayMode')) {
                    _this.comboList.splice(i, 1);
                    break;
                }
            }
        });
        this.findByName('AccountType').bind('click', function() {
            _this.findByName('GetedEmployeeId').required = this.value == '20' ? true : false;
            $('.tr_storelead')[this.value == '20' ? 'show' : 'hide']();
        });
    },
    initFormValidator: function() {
        //收取时间
        $('#' + this.id + '_GetedTime').formValidator({
            validatorgroup: this.id,
            onshow: "请输入收取时间",
            onfocus: "请输入收取时间",
            oncorrect: "填写正确"
        }).inputValidator({
            min: 1,
            empty: {
                leftempty: false,
                rightempty: false,
                emptyerror: "请输入收取时间"
            },
            onerror: "请输入收取时间"
        });
        //金额
        $('#' + this.id + '_GetedMoney').formValidator({
            validatorgroup: this.id,
            onshow: "请输入金额！",
            onfocus: "金额为必输项",
            oncorrect: " "
        }).inputValidator({
            min: 1,
            empty: {
                leftempty: false,
                rightempty: false,
                emptyerror: "金额不能为空！"
            },
            onerror: "金额不能为空！"
        }).functionValidator({ //数字验证
            fun: function(val, elem) {
                val = Chenxin.Fn.fullToHalfChar(val);
                if (parseInt(val) < 0) {
                    return false;
                }
                else {
                    return val.isNum();
                }
            },
            onerror: "请输入非0数字"
        });
    },
    createIptRow: function() {//<input class="inputbox_n" type="text" style="width:76px;" name="PayMode_'+this.drCount+'">
        this.drCount = this.drCount || 0;
        var zIndex = 30000 - this.drCount;
        $('#' + this.id + '_details').find('tbody').append('<tr><td><div class="SelectWrapSingle" style="z-index:' + zIndex + '; margin-right: 2px;" id="div_paymodel_' + this.drCount + '"></td><td><input class="inputbox_n" type="text" style="width:76px;" name="PayName_' + this.drCount + '"></td><td><input class="inputbox_n" type="text" style="width:76px;" name="PayCode_' + this.drCount + '"></td><td><input class="inputbox_n" type="text" style="width:76px;" name="PayMoney_' + this.drCount + '"></td><td><input class="inputbox_n" type="text" style="width:96%;" name="PayMemo_' + this.drCount + '"></td><td style="text-align:center;"><a title="删除" style="cursor:pointer" class="a_del_row" id="a_del_' + this.drCount + '">删除</a></td></tr>');
        this.initIptCombo();
        this.drCount++;
    },
    reset: function() {
        this.drCount = 0;
        $('#' + this.id + '_details').find('tbody').empty();
        this.comboList.splice(2, this.comboList.length - 2);
        Chenxin.UI.BillingForm.superclass.reset.call(this);
    },
    initIptCombo: function() {
        this.comboList.push(new Chenxin.UI.Combo({
            name: 'PayMode_' + this.drCount,
            containerId: '#div_paymodel_' + this.drCount,
            tbxWidth: 40,
            items: [{
                txt: '现金',
                val: '现金'
            }, {
                txt: 'POS机刷卡',
                val: 'POS机刷卡'
            }, {
                txt: '支/本票',
                val: '支/本票'
            }, {
                txt: '转账',
                val: '转账'
}]
            }));
        },
        initComponent: function() {
            Chenxin.UI.BillingForm.superclass.initComponent.call(this);
        }
    });

Chenxin.FormCommon.billingForm=function(formId){// height:100px;overflow:auto;
	return '<div colspan="4" class="x-form-title">意向信息</div><table class="tbl_f"><tr><th style="width:100px;">意向编号：</th><td><input type="hidden" name="IntentionId"><input type="text" style="width: 160px;" class="inputbox_n fl" readonly="readonly" name="IntentionCode"></td><th style="width:100px;">业务员：</th><td><input type="text" readonly="readonly" name="LastEmployee" style="width:160px;" class="inputbox_n fl"></td></tr><tr><th>房东姓名：</th><td><input type="text" readonly="readonly" name="CustomerName_Sale" style="width:160px;" class="inputbox_n fl"></td><th>客户姓名：</th><td><input type="text" name="CustomerName_Buy" readonly="readonly" id="CustomerName_Buy" style="width:160px;" class="inputbox_n fl"></td></tr><tr><th>地址：</th><td colspan="3"><input type="text" name="HouseAddress" id="HouseAddress" readonly="readonly" style="width:455px;" class="inputbox_n fl"></td></tr><tr><th>总价：</th><td colspan="3"><input type="text" name="TotalPrice" id="TotalPrice"style="width:160px;" class="inputbox_n fl" readonly="readonly"></td></tr><tr><th>入账账户：</th><td colspan="3"><input type="radio" checked="checked" name="AccountType" value="10" id="chk_company_account"><label for="chk_company_account">公司账户</label><input type="radio" name="AccountType" value="20" id="chk_personal_account"><label for="chk_personal_account">授权账户</label></td></tr><tr><th>门店：</th><td colspan="3"><div class="SelectWrapSingle" style="z-index: 2; margin-right: 2px;" id="GetedDepartId"></div></td></tr><tr class="tr_storelead" style="display:none;"><th>店长：</th><td colspan="3"><input type="text" style="width: 160px;" class="inputbox_n fl" id="GetedEmployeeId" readonly="readonly"></td></tr><tr><th>意向金总金额</th><td><input class="inputbox_n fl" type="text" id="TotalMoney" readonly="readonly" name="TotalMoney" style="width:160px"/></td><th>已开单金额</th><td><input class="inputbox_n fl" type="text" id="Text2" name="IsGetedMoney"  style="width:160px" readonly="readonly"/></td></tr><tr><th>收取时间：</th><td><input type="text" style="width: 160px;" onclick="WdatePicker({position:{left:this.getBoundingClientRect().left+1,top:this.getBoundingClientRect().top+22}});" readonly="readonly" id="'+formId+'_GetedTime" class="inputbox_n fl" name="GetedTime"></td><th>本次开单金额：</th><td><input type="text" name="GetedMoney" id="'+formId+'_GetedMoney" style="width:100px;" class="inputbox_n fl"/><div style="float: left; margin-left: 5px; width:90px;height: 26px; line-height: 26px; text-align: left;" id="'+formId+'_GetedMoneyTip"></div></td></tr></table><div class="x-wincombo-tips">注：当支付方式为“POS机刷卡”或“支/本票”时，付款方姓名和付款方帐号必填，否则该记录无效</div><div class="x-form-title">明细信息<a class="fr x-button" style="font-weight:normal;text-indent:0px;" id="btn_add_row"><span title="添加">添加</span></a></div><div id="'+formId+'_details" style="margin-bottom:10px;"><table class="tabsorternew" style="width:99%;margin:0px;margin-left:auto;margin-right:auto;"><thead><tr><th style="text-align:center;width:80px">支付方式</th><th style="text-align:center;width:80px">付款方名称</th><th style="text-align:center;width:80px">付款方帐号</th><th style="text-align:center;width:80px;">金额</th><th style="text-align:center;">备注</th><th style="text-align:center;width:60px">删除</th></tr></thead><tbody></tbody></table></div>';
}